package cn.bugstack.types.design.framework.tree;

import lombok.Getter;
import lombok.Setter;

/**
 * @description: 带异步方法的策略路由抽象类
 * @author: 刘新智
 * @date: 2025/10/28 17:19
 * @param:
 * @return:
 **/
public abstract class AbstractMultiThreadStrategyRouter<T, D, R> implements StrategyMapper<T, D, R>, StrategyHandler<T, D, R> {

    @Getter
    @Setter
    protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

    public R router(T requestParameter, D dynamicContext) throws Exception {
        // 获取下一个节点
        StrategyHandler<T, D, R> strategyHandler = this.get(requestParameter, dynamicContext);
        if (null != strategyHandler){
            return strategyHandler.apply(requestParameter, dynamicContext);
        }
        // 执行下一个节点的apply方法
        return defaultStrategyHandler.apply(requestParameter, dynamicContext);
    }

    @Override
    public R apply(T requestParameter, D dynamicContext) throws Exception {
        // 异步加载数据
        multiThread(requestParameter, dynamicContext);
        // 逻辑受理
        return doApply(requestParameter, dynamicContext);
    }

    protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;

    protected abstract void multiThread(T requestParameter, D dynamicContext) throws Exception;


}
